package com.intellagric.service.traceability.Impl.trace_supplier;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceSupplierMapper;
import com.intellagric.mapper.TraceSupplierMapper;
import com.intellagric.module.traceability.SupplierVo;
import com.intellagric.pojo.TraceSupplier;
import com.intellagric.pojo.TraceSupplierExample;
import com.intellagric.service.traceability.trace_supplier.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/6
 * @DESC： 公司信息管理服务实现
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private TraceSupplierMapper supplierMapper;

    /**
     * 添加公司信息
     * @param supplier
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addSupplier(TraceSupplier supplier) {
        if (supplierMapper.insertSelective(supplier) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除公司信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteSupplier(String id) {
        if (supplierMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改公司信息
     * @param supplier
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editSupplier(TraceSupplier supplier) {
        if (supplierMapper.updateByPrimaryKeySelective(supplier) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询公司信息
     * @param id
     * @return TraceSupplier
     */
    @Override
    public TraceSupplier getSupplierById(String id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询公司信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSupplierList() {
        TraceSupplierExample supplierExample = new TraceSupplierExample();
        supplierExample.createCriteria().andIdIsNotNull();
        List<TraceSupplier> supplierList = supplierMapper.selectByExample(supplierExample);
        return LayuiDataGridResult.success().add(supplierList,supplierList.size());
    }

    /**
     * 分页查询公司信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSupplierPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceSupplierExample supplierExample = new TraceSupplierExample();
        supplierExample.createCriteria().andIdIsNotNull();
        List<TraceSupplier> supplierList = supplierMapper.selectByExample(supplierExample);
        PageInfo<TraceSupplier> pageInfo = new PageInfo<>(supplierList);
        return LayuiDataGridResult.success().add(supplierList,pageInfo.getTotal());
    }

    /**
     * 通过Vo类型查询公司信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSupplierByVo(int page, int rows,String companeName) {
        PageHelper.startPage(page,rows);
        TraceSupplierExample supplierExample = new TraceSupplierExample();
        if(companeName==null)
            companeName="";
            supplierExample.createCriteria().andCompaneNameLike("%"+  companeName+ "%");
        List<TraceSupplier> supplierList = supplierMapper.selectByExample(supplierExample);
        PageInfo<TraceSupplier> pageInfo = new PageInfo<>(supplierList);
        return LayuiDataGridResult.success().add(supplierList,pageInfo.getTotal());
    }
}
